package com.qh.book.controller;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import com.qh.book.converter.OrderMaster2OrderDTO;
import com.qh.book.dataobj.OrderDetail;
import com.qh.book.dataobj.OrderMaster;
import com.qh.book.dto.OrderDTO;
import com.qh.book.enums.ResultEnum;
import com.qh.book.exception.BsException;
import com.qh.book.service.OrderMasterService;

/**
 * 
 * @author qihong
 * @date 2017年10月7日 下午4:56:37 
 * @Description: TODO
 */
@Controller
@RequestMapping("/backstage/order")
public class OrderController {
	
	@Autowired
	private OrderMasterService orderMasterService;

	/**
	 * 
	 * 返回总订单表 
	 * @param page
	 * @param size
	 * @param map
	 * @return
	 * @author qihong
	 */
	@GetMapping("/list")
	public ModelAndView getList(@RequestParam(value = "page",defaultValue = "1")Integer page,
								@RequestParam(value="size",defaultValue = "10")Integer size,
								Map<String,Object> map) {
		
		Pageable pageable = new PageRequest(page - 1, size);
		Page<OrderMaster> orderPage = orderMasterService.findList(pageable);
		List<OrderMaster> orderList = orderPage.getContent();
		List<OrderDTO> orderDTOList = OrderMaster2OrderDTO.convert(orderList);
		long totalPages = orderPage.getTotalElements();
		map.put("orderDTOList", orderDTOList);
		map.put("totalPages", totalPages);
		map.put("currentpage", page);
		map.put("size", size);
		return new ModelAndView("order/list", map);
		
		
	}
	
	
	@GetMapping("/detailoforder")
	public ModelAndView getOrderDetailById(@RequestParam(value="orderid") String orderId,
										Map<String,Object> map) {
		
		OrderDTO orderDTO = new OrderDTO();
		try {
			orderDTO = orderMasterService.findOne(orderId);
		}catch(BsException e) {
			map.put("error", e.getMessage());
		}
		map.put("orderDTO", orderDTO);
		return new ModelAndView("/order/detailoforder",map);
	}
}

